package com.depth.viewer.request
{
	import com.depth.viewer.events.AppEvent;
	import com.depth.viewer.events.EventBus;
	import com.depth.viewer.facade.IocAppFacade;
	import com.depth.viewer.request.http.HttpRequestUtil;
	import com.depth.viewer.request.page.FZFPage;
	
	import flash.external.ExternalInterface;
	
	import mx.managers.BrowserManager;
	import mx.messaging.messages.HTTPRequestMessage;

	public class HttpPoolUtil
	{
		private static var _instance:HttpPoolUtil;
		private var _rootUrl:String;
		
		/**
		 * 是否需要弹窗
		 * */
		private var _isPopUp:Boolean = true;
		private var _sumOfError:int = 0;
		private static const MAX:int = 100;
		
		public function HttpPoolUtil(url:String=null)
		{
			init(url);
		}
		
		
		public static function get instance():HttpPoolUtil{
			if(_instance==null){
				_instance = new HttpPoolUtil();
			}
			return _instance;
		}
		
		public static function getInstance(url:String=null):HttpPoolUtil{
			if(_instance==null){
				_instance = new HttpPoolUtil(url);
			}
			return _instance;
		}
		/**
		 * 这里是根据配置文件，判断是否使用动态url地址
		 */
		private function init(url:String=null):void{
			_rootUrl = url?url:IocAppFacade.getInstance().getObject("rootUrl") as String;
		}
		
		/**
		 * 走指定的url、
		 * @param id 配置文件URL的ID
		 * @param resultHandler  返回的方法
		 * @param methodName 所要调用的方法名称
		 * @param args  参数列表
		 * */
		public function reuqestURL(url:String,resultHandler:Function,type:String = HTTPRequestMessage.GET_METHOD,params:Object=null):void{
			var http:HttpRequestUtil  = new HttpRequestUtil();
			if(type == HTTPRequestMessage.GET_METHOD){
				http.httpGet(url,params,resultHandler,faultHanlder);
			}else{
				http.httpPost(url,params,resultHandler,faultHanlder);
			}
			
		}
		
	
		/**
		 * @param id 配置文件URL的ID
		 * @param resultHandler  返回的方法
		 * @param methodName 所要调用的方法名称
		 * @param args  参数列表
		 * */
		public function reuqest(url:String,resultHandler:Function,type:String = HTTPRequestMessage.GET_METHOD,params:Object=null):void{
			var http:HttpRequestUtil  = new HttpRequestUtil();
			url += (url.indexOf("?")!=-1?"&":"?");
			url += "random="+Math.round(Math.random()*1000000); 
			if(type == HTTPRequestMessage.GET_METHOD){
				http.httpGet(_rootUrl + url,params,resultHandler,faultHanlder);
			}else{
				http.httpPost(_rootUrl + url,params,resultHandler,faultHanlder);
			}
			
		}
		
		/**
		 * 可以返回自带的data
		 * @param id 配置文件URL的ID
		 * @param resultHandler  返回的方法
		 * @param methodName 所要调用的方法名称
		 * @param data 需要传递的方法
		 * @param args  参数列表
		 * */
		public function reuqestData(url:String,resultHandler:Function,data:Object,type:String = HTTPRequestMessage.GET_METHOD,params:Object=null):void{
			var http:HttpRequestUtil  = new HttpRequestUtil(data);
			if(type == HTTPRequestMessage.GET_METHOD){
				http.httpGet(_rootUrl + url,params,resultHandler,faultHanlder);
			}else{
				http.httpPost(_rootUrl + url,params,resultHandler,faultHanlder);
			}
			
		}
		/**
		 * 服务器故障
		 */
		private function faultHanlder(message:String):void
		{
			trace(message);
			if(++_sumOfError>MAX&&_isPopUp){
				//停止所有的定时器
				EventBus.getInstance().dispatchEvent(new AppEvent(AppEvent.NETWORK_ERROR));
				new FZFPage().showErrorInfo();
				_isPopUp = false;
			}
		}
		
		/**
		 * 错误次数
		 * */
		public function set sumOfError(value:int):void
		{
			_sumOfError = value;
		}
	}
}